package com.example.projecth.water2;

import com.example.projecth.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class WaterRegister extends Activity {

	private final int TAG_OK=1;
	private EditText name2, weight2, input;
	private Button ok, cancle, but2;
	private RadioButton radiobutton1;
	private RadioButton radiobutton2;
	private RadioButton radiobutton3;
	private RadioButton radiobutton4;
	private RadioButton radiobutton5;
	private RadioButton radiobutton6;

	private RadioGroup radio;

	public int i;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.w_water_register_view);
		
		name2 = (EditText) findViewById(R.id.name2);
		weight2 = (EditText) findViewById(R.id.weight2);
		ok = (Button) findViewById(R.id.ok);
		radiobutton6=(RadioButton)findViewById(R.id.radioButton6);
		
		radio= (RadioGroup)findViewById(R.id.radio);
		radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				switch(checkedId){
				case R.id.radioButton1 :
					i =50;
					
					break;
				case R.id.radioButton2 :
					i =150;
					
					break;
				case R.id.radioButton3 :
					i =200;
					
					break;
				case R.id.radioButton4 :
					i =350;
					
					break;
				case R.id.radioButton5 :
					i =500;
					
					break;
				case R.id.radioButton6 :
					showCustomDialog();
					break;
					
				}
				
			}
		});  
		ok.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){   
            	Memo memo = new Memo();
        		//int tag = Integer.parseInt(v.getTag().toString());
        		if (!name2.getText().toString().equals("") && !weight2.getText().toString().equals("")) {
        			
        			memo.setName2(name2.getText().toString());
        			memo.setRadio(i);
        			memo.setWeight2(Integer.valueOf(weight2.getText().toString()));
        			
        			Intent intent = new Intent(WaterRegister.this, WaterCheck.class);
        			intent.putExtra("newmemo", memo);
        			setResult(TAG_OK,intent);
        			
        			finish();
        		}  
            }
        });
		return ;
	};

	private void showCustomDialog() {		
		//1. layout _xml을 객체로 변경(inflate)
		LayoutInflater inflater =this.getLayoutInflater();
		View view = inflater.inflate(R.layout.w_select, null);
		
		input =(EditText)view.findViewById(R.id.input);
		//final EditText pwd=(EditText)view.findViewById(R.id.input);
		Button but2 =(Button)view.findViewById(R.id.but2);
		
		//2.  변경된 layout 을 Dialog에 적용
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final AlertDialog dlg = builder.setTitle("?")
			   .setIcon(R.drawable.watercup)
			   .setView(view)
			   .setCancelable(true)
			   .show();
		
		but2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if (!input.getText().toString().equals("")) {
				i = Integer.valueOf(input.getText().toString());
				Toast.makeText(WaterRegister.this, String.format("%d", i), Toast.LENGTH_SHORT).show();
				dlg.dismiss();
				} else if (input.getText().toString().equals("")){
					Toast.makeText(WaterRegister.this, "값을 입력해 주세요/", Toast.LENGTH_SHORT).show();
					dlg.dismiss();
				
				}
			
			}
		});
	
	}
	
	
}